<?php
App::uses('AppModel', 'Model');
/**
 * VoxBloc Model
 *
 * @property bloc_question $bloc_question
 */
class VoxBloc extends AppModel {
	// ajout d'une méthode pour récupérer tout les vox_blocs avec leurs blocs de questions et leurs questions
	public $findMethods = array('QuestionAssociees' => true);
	
	public function desactiverToutLesVoxBlocs() {
		$this->query("UPDATE vox_blocs SET active=0");
	}
	
	public function obtenirToutLesBlocsEtLeursQuestions() {
		$result = $this->query("SELECT * FROM vox_blocs VoxBloc ".
								"JOIN bloc_questions BlocQuestion ON VoxBloc.id_vox_bloc = BlocQuestion.id_vox_bloc ".
								"JOIN questions Question ON BlocQuestion.id_bloc = Question.id_bloc ".
								"LEFT JOIN reponses Reponse ON Question.id_question = Reponse.id_question ".
								"WHERE VoxBloc.active AND BlocQuestion.active AND Question.active ".
								"ORDER BY VoxBloc.rang_vox_bloc, BlocQuestion.rang_bloc, Question.rang_question, Reponse.rang_reponse");
		
		$out = Array();
		$id_question = Array();
		$nb_question = 0;
		
		foreach($result as $r) {
			// on crée l'array du VoxBloc si il n'existe pas déjà
			if(!isset($out[$r['VoxBloc']['rang_vox_bloc']])) {
				$out[$r['VoxBloc']['rang_vox_bloc']] = Array();
				$out[$r['VoxBloc']['rang_vox_bloc']]['info_VoxBloc'] = $r['VoxBloc'];
				$out[$r['VoxBloc']['rang_vox_bloc']]['BlocsQuestions'] = Array();
			}
			// on crée l'array du BlocQuestion si il n'existe pas déjà
			if(!isset($out[$r['VoxBloc']['rang_vox_bloc']]['BlocsQuestions'][$r['BlocQuestion']['rang_bloc']])) {
				$out[$r['VoxBloc']['rang_vox_bloc']]['BlocsQuestions'][$r['BlocQuestion']['rang_bloc']] = Array();
				$out[$r['VoxBloc']['rang_vox_bloc']]['BlocsQuestions'][$r['BlocQuestion']['rang_bloc']]['info_BlocQuestion'] = $r['BlocQuestion'];
				$out[$r['VoxBloc']['rang_vox_bloc']]['BlocsQuestions'][$r['BlocQuestion']['rang_bloc']]['Questions'] = Array();
			}
			
			// si la question n'est pas encore dans la liste
			if(!isset($id_question[$r['Question']['id_question']])) {
				// on incrément le numéro de la question à traiter
				$nb_question ++;
				// on indique que cette question existe
				$id_question[$r['Question']['id_question']] = true;
				// on ajoute la question à la liste
				$out[$r['VoxBloc']['rang_vox_bloc']]['BlocsQuestions'][$r['BlocQuestion']['rang_bloc']]['Questions'][$r['Question']['rang_question']]['info'] = $r['Question'];
			}
			
			// si un élément reponse existe, on l'ajoute
			if(isset($r['Reponse']['id_reponse']) && $r['Reponse']['id_reponse'] != NULL) {
				// on test si une réponse à déjà été ajouté, et on crée un array si ce n'est pas le cas
				if(!isset($out[$r['VoxBloc']['rang_vox_bloc']]['BlocsQuestions'][$r['BlocQuestion']['rang_bloc']]['Questions'][$r['Question']['rang_question']]['Reponses'])) {
					$out[$r['VoxBloc']['rang_vox_bloc']]['BlocsQuestions'][$r['BlocQuestion']['rang_bloc']]['Questions'][$r['Question']['rang_question']]['Reponses'] = Array();
				}
				// on ajoute la réponse à la liste de réponse pour cette question
				$out[$r['VoxBloc']['rang_vox_bloc']]['BlocsQuestions'][$r['BlocQuestion']['rang_bloc']]['Questions'][$r['Question']['rang_question']]['Reponses'][] = $r['Reponse'];
			}
		}
		
		return $out;
	}
	
	public function trouverToutLesBlocsEtLeursQuestions() {
		$result = $this->query("SELECT * FROM vox_blocs VoxBloc ".
								"JOIN bloc_questions BlocQuestion ON VoxBloc.id_vox_bloc = BlocQuestion.id_vox_bloc ".
								"JOIN questions Question ON BlocQuestion.id_bloc = Question.id_bloc ".
								"LEFT JOIN reponses Reponse ON Question.id_question = Reponse.id_question ".
								"WHERE VoxBloc.active AND BlocQuestion.active AND Question.active ".
								"ORDER BY VoxBloc.rang_vox_bloc, BlocQuestion.rang_bloc, Question.rang_question, Reponse.rang_reponse");
		
		$out = Array();
		$id_question = Array();
		$nb_question = 0;
		
		foreach($result as $r) {
			// on crée l'array du VoxBloc si il n'existe pas déjà
			if(!isset($out[$r['VoxBloc']['id_vox_bloc']])) {
				$out[$r['VoxBloc']['id_vox_bloc']] = Array();
				$out[$r['VoxBloc']['id_vox_bloc']]['info_VoxBloc'] = $r['VoxBloc'];
				$out[$r['VoxBloc']['id_vox_bloc']]['BlocsQuestions'] = Array();
			}
			// on crée l'array du BlocQuestion si il n'existe pas déjà
			if(!isset($out[$r['VoxBloc']['id_vox_bloc']]['BlocsQuestions'][$r['BlocQuestion']['id_bloc']])) {
				$out[$r['VoxBloc']['id_vox_bloc']]['BlocsQuestions'][$r['BlocQuestion']['id_bloc']] = Array();
				$out[$r['VoxBloc']['id_vox_bloc']]['BlocsQuestions'][$r['BlocQuestion']['id_bloc']]['info_BlocQuestion'] = $r['BlocQuestion'];
				$out[$r['VoxBloc']['id_vox_bloc']]['BlocsQuestions'][$r['BlocQuestion']['id_bloc']]['Questions'] = Array();
			}
			
			// si la question n'est pas encore dans la iste
			if(!isset($id_question[$r['Question']['id_question']])) {
				// on incrément le numéro de la question à traiter
				$nb_question ++;
				// on indique que cette question existe
				$id_question[$r['Question']['id_question']] = true;
				// on ajoute la question à la liste
				$out[$r['VoxBloc']['id_vox_bloc']]['BlocsQuestions'][$r['BlocQuestion']['id_bloc']]['Questions'][$nb_question]['info'] = $r['Question'];
			}
			
			// si un élément reponse existe, on l'ajoute
			if(isset($r['Reponse']['id_reponse']) && $r['Reponse']['id_reponse'] != NULL) {
				// on test si une réponse à déjà été ajouté, et on crée un array si ce n'est pas le cas
				if(!isset($out[$r['VoxBloc']['id_vox_bloc']]['BlocsQuestions'][$r['BlocQuestion']['id_bloc']]['Questions'][$nb_question]['Reponses'])) {
					$out[$r['VoxBloc']['id_vox_bloc']]['BlocsQuestions'][$r['BlocQuestion']['id_bloc']]['Questions'][$nb_question]['Reponses'] = Array();
				}
				// on ajoute la réponse à la liste de réponse pour cette question
				$out[$r['VoxBloc']['id_vox_bloc']]['BlocsQuestions'][$r['BlocQuestion']['id_bloc']]['Questions'][$nb_question]['Reponses'][] = $r['Reponse'];
			}
			
			// si un élément nouveau_statut existe, on l'ajoute
			if(isset($r['Nouveau_statut']['id_reponse']) && $r['Nouveau_statut']['id_reponse'] != NULL) {
				// on test si un nouveau statut à déjà été ajouté, et on crée un array si ce n'est pas le cas
				if(!isset($out[$r['VoxBloc']['id_vox_bloc']]['BlocsQuestions'][$r['BlocQuestion']['id_bloc']]['Questions'][$nb_question]['NouveauStatut'])) {
					$out[$r['VoxBloc']['id_vox_bloc']]['BlocsQuestions'][$r['BlocQuestion']['id_bloc']]['Questions'][$nb_question]['NouveauStatut'] = Array();
				}
				// on ajoute le nouveau statut a la liste des nouveaux status
				$out[$r['VoxBloc']['id_vox_bloc']]['BlocsQuestions'][$r['BlocQuestion']['id_bloc']]['Questions'][$nb_question]['NouveauStatut'][] = $r['NouveauStatut'];
			}
		}
		
		return $out;
	}
/**
 * Primary key field
 *
 * @var string
 */
	public $primaryKey = 'id_vox_bloc';
/**
 * Display field
 *
 * @var string
 */
	public $displayField = 'libelle_vox_bloc';
/**
 * Validation rules
 *
 * @var array
 */
	public $validate = array(
		'id_vox_bloc' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'rang_vox_bloc' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);

	//The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * hasMany associations
 *
 * @var array
 */
	public $hasMany = array(
		'bloc_question' => array(
			'className' => 'bloc_question',
			'foreignKey' => 'id_vox_bloc',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		)
	);

}
